package net.shopxx.action.admin;


import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.entity.StoreNavigation;
import net.shopxx.service.StoreNavigationService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.BeanUtils;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 旗舰店
 */

@ParentPackage("admin")
public class StorenavigationAction extends BaseAdminAction {


	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7030575494433759768L;


	private StoreNavigation storeNavigation;
	

	@Resource
	private StoreNavigationService storeNavigationService;



	// 列表
	public String list() {
		pager = storeNavigationService.findByPager(pager);
		return LIST;
	}
	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		storeNavigation = storeNavigationService.load(id); 
		return INPUT;
	}
	
	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "storeNavigation.name", message = "名称不允许为空!"),
		}
	)
	@InputConfig(resultName = "error")
	public String save() throws Exception {
		storeNavigationService.save(storeNavigation);
		flushCache();
		redirectionUrl = "storenavigation!list.action";
		return SUCCESS;
	}
	
	
	// 更新
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "storeNavigation.name", message = "名称不允许为空!"),
			
		}
	)
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		StoreNavigation persistent = storeNavigationService.load(id); 
		BeanUtils.copyProperties(storeNavigation, persistent, new String[] {"id", "createDate", "modifyDate"});
		storeNavigationService.update(persistent);
		flushCache();
		redirectionUrl = "storenavigation!list.action";
		return SUCCESS;
	}
	

	// 删除
	public String delete() throws Exception {
		storeNavigationService.delete(ids);
		flushCache();
		return ajaxJsonSuccessMessage("删除成功！");
	}
	public StoreNavigation getStoreNavigation() {
		return storeNavigation;
	}

	public void setStoreNavigation(StoreNavigation storeNavigation) {
		this.storeNavigation = storeNavigation;
	}
	
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}
	

}